<?php

/**
 * This is the model class for table "cad_parcela".
 *
 * The followings are the available columns in table 'cad_parcela':
 * @property integer $cod_parcela
 * @property string $nome
 * @property integer $cod_entrada
 * @property integer $status
 * @property integer $parcela
 */
class cad_parcela extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return cad_parcela the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cad_parcela';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cod_entrada, status, parcela', 'numerical', 'integerOnly'=>true),
			array('nome, data, vencimento, valor', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cod_parcela,data, nome, cod_entrada, status, parcela', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cod_entrada0' => array(self::BELONGS_TO, 'CadEntradas', 'cod_entrada'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cod_parcela' => 'Parcela',
			'nome'        => 'Descrição',
			'cod_entrada' => 'Entrada',
			'status'      => 'Status',
			'parcela'     => 'Parcela',
                        'valor'       => 'Valor',  
                        'data'        => 'Data',
                        'vencimento'  => 'Vencimento',
		);
	}

        
        public function geraParcelas($_parcelas,$valor,$cod_entrada,$nome){
            
                           $_valor  = ($valor / $_parcelas);
                           $_ano    = date('Y'); 
                           $_mes    = date('m'); 
                           $_dia    = '01';//date('d'); 
                           $_hora   = date("H:i:s");                            

                          for ($i = 1; $i <= $_parcelas; $i++) {                           
                            $_mes++;
                            $parcela              = new cad_parcela;
                            $parcela->nome        = $nome;
                            $parcela->cod_entrada = $cod_entrada;
                            $parcela->parcela     = $i;
                            $parcela->valor       = $_valor;
                            $parcela->vencimento  = $_ano.'-'.$_mes.'-'.$_dia.' '.$_hora;  
                            
                            $parcela->save();
                            
                          }             
            
        }

                
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('cod_parcela',$this->cod_parcela);

		$criteria->compare('nome',$this->nome,true);

		$criteria->compare('cod_entrada',$this->cod_entrada);

		$criteria->compare('status',$this->status);

		$criteria->compare('parcela',$this->parcela);

                $criteria->compare('data',$this->data);  
                 
                $criteria->compare('vencimento',$this->vencimento); 
                
		return new CActiveDataProvider('cad_parcela', array(
			'criteria'=>$criteria,
		));
	}
}